package com.hong.cloud.mall.practice.categoryproduct.mq;

import com.hong.cloud.mall.practice.categoryproduct.model.pojo.Product;
import com.hong.cloud.mall.practice.categoryproduct.service.ProductService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 描述：接收者
 */
@Component
@RabbitListener(queues = "queue1")
public class Receiver {
    @Autowired
    private ProductService productService;

    @RabbitHandler
    public void process(String msg) {
        System.out.println("接收到消息：" + msg);
        // 消息格式: productId,stock
        String[] strings = msg.split(",");
        Integer productId = Integer.valueOf(strings[0]);
        Integer stock = Integer.valueOf(strings[1]);
        productService.updateStock(productId, stock);
    }
}
